﻿using System;
using System.Collections.Generic;
using System.Net;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO;

namespace CloudStorageLight.WpfClient
{
    /// <summary>
    /// ファイルアイテム
    /// </summary>
    public class FileItem : TreeItem
    {

        public override string Title
        {
            get
            {
                if (base.Title == null && this.Path != null) return this.Path.Split('/').LastOrDefault();
                return base.Title;
            }
            set
            {
                base.Title = value;
                SetIcon(value);
            }
        }

        private void SetIcon(string value)
        {
            if (value != null)
            {
                var ext = System.IO.Path.GetExtension(value);
                switch (ext.ToLower())
                {
                    case ".xlsx":
                    case ".xls":
                        Icon = "/CloudStorageLight;Component/Images/page_white_excel.png";
                        break;
                    case ".doc":
                    case ".docx":
                        Icon = "/CloudStorageLight;Component/Images/page_white_word.png";
                        break;
                    case ".ppt":
                    case ".pptx":
                        Icon = "/CloudStorageLight;Component/Images/page_white_powerpoint.png";
                        break;
                    case ".pdf":
                        Icon = "/CloudStorageLight;Component/Images/page_white_acrobat.png";
                        break;
                    case ".jpeg":
                    case ".jpg":
                    case ".gif":
                        Icon = "/CloudStorageLight;Component/Images/page_white_picture.png";
                        break;
                    case ".txt":
                        Icon = "/CloudStorageLight;Component/Images/page_white_text.png";
                        break;
                    case ".zip":
                        Icon = "/CloudStorageLight;Component/Images/page_white_zip.png";
                        break;
                    default:
                        Icon = "/CloudStorageLight;Component/Images/page_white.png";
                        break;

                }
            }
            else
            {
                Icon = "/CloudStorageLight;Component/Images/page_white.png";
            }
        }

        public ImageSource  ExeIcon
        {
            get 
            {
                var ext = System.IO.Path.GetExtension(Title);
                if (!exeIcons.ContainsKey(ext))
                {
                    try
                    {
                        var path = Utils.FileExtentionInfo(Utils.AssocStr.Executable, ext);
                        //System.Drawing.Icon icon = System.Drawing.Icon.ExtractAssociatedIcon(path);
                        System.Drawing.Icon icon = IconTools.GetIconForExtension(ext, ShellIconSize.LargeIcon);
                        exeIcons[ext] = System.Windows.Interop.Imaging.CreateBitmapSourceFromHIcon(
                                    icon.Handle,
                                    new Int32Rect(0, 0, icon.Width, icon.Height),
                                    System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
                    }
                    catch (Exception)
                    {

                        exeIcons[ext] = null;
                    }
                }
                return exeIcons[ext];
            }
        }
        private static Dictionary<string, ImageSource> exeIcons = new Dictionary<string, ImageSource>();

        public string OptionIcon
        {
            get { return IsLocalEditing ? "/CloudStorageLight;Component/Images/editing.png" : "/CloudStorageLight;Component/Images/blank.png"; }
        }

        public bool IsLocalEditing
        {
            get
            {
                var cache = BlobDataManager.Instance.GetCachedFile(this);
                if (cache == null) return false;
                return cache.IsDirty || IsCheckout;
            }
        }

        public bool IsLocalOnly { get; set; }

        public FileItem()
        {
            Type = "File";
            
        }
        public string SearchContent
        {
            get { return _searchContent; }
            set { _searchContent = value; OnPropertyChanged("SearchContent", "HasSearchContent"); }
        }
        private string _searchContent;

        public bool HasSearchContent
        {
            get { return !string.IsNullOrEmpty(SearchContent); }
        }

        public string CheckoutUser
        {
            get { return _CheckoutUser; }
            set { _CheckoutUser = value; OnPropertyChanged("CheckoutUser", "CanCheckin", "CanCheckout"); }
        }
        private string _CheckoutUser;


        public string Remark
        {
            get { return _Remark; }
            set { _Remark = value; OnPropertyChanged("Remark"); }
        }
        private string _Remark;

        public string Snapshot { get; set; }

        public bool IsReadOnly { get; set; }

        public bool VisibleContent { get { return !string.IsNullOrEmpty(SearchContent); } }
        public string DownloadMode
        {
            get { return "attachment"; }
        }

        public bool IsOfficeFileExt
        {
            get 
            {
                var lowerPath = Path.ToLower();
                if (lowerPath.EndsWith(".pptx") || lowerPath.EndsWith(".docx") || lowerPath.EndsWith(".xlsx"))
                {
                    return true;
                }
                return false;
            }
        }
        public bool Downloading
        {
            get { return _downloading; }
            set { _downloading = value; OnPropertyChanged("Downloading"); }
        }
        private bool _downloading;

        public bool CanModify
        {
            get { return string.IsNullOrEmpty(this.CheckoutUser) || CheckoutUser == BlobDataManager.Instance.LoginUser; }
        }

        public bool IsCheckout
        {
            get { return string.Equals(this.CheckoutUser, BlobDataManager.Instance.LoginUser, StringComparison.InvariantCultureIgnoreCase); }
        }

        public bool CanCheckin
        {
            get { return IsCheckout && !BlobDataManager.Instance.Offline; }
        }

        public bool CanUpload
        {
            get
            {
                if (BlobDataManager.Instance.Offline) return false;
                var local = BlobDataManager.Instance.GetLocalFilePath(this);
                if (local == null) return false;
                if (!File.Exists(local)) return false; 
                return true;
            }
        }


        public bool CanCheckout
        {
            get { return !IsLocalOnly && string.IsNullOrEmpty(this.CheckoutUser) && !BlobDataManager.Instance.Offline; }
        }
        public bool CanDownload
        {
            get
            {
                if (BlobDataManager.Instance.Offline) return false;
                if (IsLocalOnly) return false;
                return true;
            }
        }

        public bool CanCopy { get { return !BlobDataManager.Instance.Offline; } }
        public bool CanPaste { get { return !BlobDataManager.Instance.Offline; } }
        public bool CanCut { get { return !BlobDataManager.Instance.Offline; } }
        public bool CanSave { get { return !BlobDataManager.Instance.Offline; } }
        public bool CanOpen { get { return !BlobDataManager.Instance.Offline; } }
        public bool CanShowUrl { get { return !BlobDataManager.Instance.Offline; } }
        public bool CanShowHistory { get { return !BlobDataManager.Instance.Offline; } }
        public bool CanDelete { get { return !BlobDataManager.Instance.Offline; } }
        public bool CanRename { get { return !BlobDataManager.Instance.Offline && !IsLocalEditing; } }
        public bool CanDeleteCompletely { get { return !BlobDataManager.Instance.Offline; } }

        public bool CanDeltaDownload
        {
            get 
            { 
                if (BlobDataManager.Instance.Offline) return false;
                var cache = BlobDataManager.Instance.GetCachedFile(this);
                if (cache == null) return false;
                if (cache.SnapshotTime == null) return false;
                return true;
            }
        }

        public string Message
        {
            get { return _Message; }
            set { _Message = value; OnPropertyChanged("Message"); }
        }
        private string _Message;

    }
}
